<template>
  <div id="login">
    <div v-if="$store.state.loginFlag">
      <img src="@/images/login_bg.jpg" class="bgImg">
    </div>
    <div v-else @click="login1" title="operation">
      <img src="@/images/login_bg.jpg" class="bgImg">
    </div>
    <div class="login-box bg-color-white" v-if="$store.state.loginFlag">
      <p class="login-box-title">{{ login_title }}</p>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
        <el-form-item prop="userName">
          <el-input v-model="ruleForm.userName" placeholder="用户名123"></el-input>
        </el-form-item>
        <el-form-item prop="passWord">
          <el-input v-model="ruleForm.passWord" type="password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="disabled" @click="login('ruleForm')" @keyup.enter.native="login">登  陆</el-button>
        </el-form-item>
        <!--<el-form-item v-if="!$store.state.loginFlag">
          <el-button type="primary" :disabled="disabled" @click="login('ruleForm')" @keyup.enter.native="login">进  入</el-button>
        </el-form-item>-->
        <el-form-item>
          <el-checkbox v-model="checked">记住我的账号</el-checkbox>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  import sysNameObj from '../../sysNameObj'
  import { nextPage } from '../../assets/js/index'
  import ajax from '../../api/ajax/ajax'
  import api from '../../api/url'
  import store from '../../store/index'
  import { mapMutations } from 'vuex'

  export default {
    name: 'login',
    data () {
      let userNameValidator = (rule, value, callback) => {
        if (value === 'null') callback(new Error('null不能作为用户名存在！'))
        callback()
      }
      return {
        login_title: '',
        ruleForm: {
          userName: '',
          passWord: ''
        },
        checked: false,
        rules: {
          userName: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 1, max: 32, message: '长度在 1 到 32 个字符', trigger: 'blur' },
            { validator: userNameValidator }
          ],
          passWord: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 32, message: '长度在 6 到 32 个字符', trigger: 'blur' }
          ]
        },
        disabled: false
      }
    },
    methods: {
      ...mapMutations([
        'pd_updateLoginFlag'
      ]),
      login1 () {
        if (!(document.cookie || navigator.cookieEnabled)) {
          console.warn('开启Cookie之后才能登录!')
          return false
        }
        if (!this.disabled) {
          this.disabled = true
          const paramsData = {
            username: this.ruleForm.userName,
            password: this.ruleForm.passWord
          }
          ajax.post(api.LOGIN, paramsData).then((res) => {
            store.commit('updateusername', res.data.user_code)
            store.commit('userId', res.data.user_id)
            if (this.checked) {
              localStorage.setItem('userName', this.ruleForm.userName)
              localStorage.setItem('checked', true)
            } else {
              localStorage.removeItem('userName')
              localStorage.removeItem('checked')
            }
            nextPage('/systemList')
          }).catch(req => {
            this.disabled = false
          })
        }
      },
      login (formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (!(document.cookie || navigator.cookieEnabled)) {
              console.warn('开启Cookie之后才能登录!')
              return false
            }
            if (!this.disabled) {
              this.disabled = true
              const paramsData = {
                username: this.ruleForm.userName,
                password: this.ruleForm.passWord
              }
              ajax.post(api.LOGIN, paramsData).then((res) => {
                store.commit('updateusername', res.data.user_code)
                store.commit('userId', res.data.user_id)
                if (this.checked) {
                  localStorage.setItem('userName', this.ruleForm.userName)
                  localStorage.setItem('checked', true)
                } else {
                  localStorage.removeItem('userName')
                  localStorage.removeItem('checked')
                }
                nextPage('/systemList')
              }).catch(req => {
                this.disabled = false
              })
            }
          } else {
            return false
          }
        })
      }
    },
    created () {
      this.login_title = sysNameObj.self.login_title
      if ('userName' in localStorage && 'checked' in localStorage) {
        this.userName = localStorage.getItem('userName')
        this.checked = Boolean(localStorage.getItem('checked'))
      }
      if (this.$route.query.userName) {
        this.pd_updateLoginFlag(false)
        this.ruleForm.userName = this.$route.query.userName
        this.ruleForm.passWord = this.$route.query.passWord
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  #login
    height 100%
    position relative
    .bgImg
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
    .login-box
      position absolute
      top 50%
      margin-top -195px
      right 15%
      width 470px
      padding 10px 0 10px 0
      border 1px solid #E1E2E2
      background: transparent;
      .login-box-title
        font-size 32px
        text-align center
        margin 30px 0
        /*color #016DFB*/
        color #fff
        font-weight bold
      .demo-ruleForm
        padding 0 50px
        .el-input__inner
          border-radius 0
          border-top transparent
          border-left transparent
          border-right transparent
        .el-button
          width 100%
          margin-top 10px
          background #409EFF
    input:-webkit-autofill , textarea:-webkit-autofill, select:-webkit-autofill
      -webkit-text-fill-color #606266 !important
      -webkit-box-shadow 0 0 0 1000px transparent  inset !important
      background-color #fff
      background-image none
      transition background-color 50000s ease-in-out 0s //背景色透明  生效时长  过渡效果  启用时延迟的时间
    input
      background-color #fff
    .el-checkbox__label
     color: #fff;
</style>
